<?php
namespace app\model;

use think\Model;

class SystemConfig extends Model
{
    // 设置表名
    protected $name = 'system_configs';
    
    // 自动时间戳类型
    protected $autoWriteTimestamp = 'int';
    
    // 时间字段
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    
    // 字段类型转换
    protected $type = [
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'delete_time' => 'integer',
        'sort_order' => 'integer'
    ];

    /**
     * 获取配置值
     * @param string $key 配置键名
     * @param mixed $default 默认值
     * @return mixed
     */
    public static function getConfig($key, $default = null)
    {
        $config = self::where('config_key', $key)->withoutField('delete_time')->find();
        return $config ? $config->config_value : $default;
    }

    /**
     * 获取分组配置
     * @param string $group 配置分组
     * @return array
     */
    public static function getGroupConfigs($group)
    {
        return self::where('config_group', $group)
            ->withoutField('delete_time')
            ->order('sort_order', 'asc')
            ->select()
            ->toArray();
    }

    /**
     * 批量更新配置
     * @param array $configs 配置数组
     * @return bool
     */
    public static function updateConfigs($configs)
    {
        foreach ($configs as $key => $value) {
            self::where('config_key', $key)->update(['config_value' => $value]);
        }
        return true;
    }
} 